// pages/play/play.js
const{getMusicPlayDetail} = require("../../models/playlistData")
/* 1.获取播放器 */
const audio = wx.getBackgroundAudioManager();
Page({
  data: {
    play:"",
    /* 定义播放的状态 */
    playState:true
  },

  onLoad: function (options) {
    var {id} = options;
    // console.log(id);
    let imgUrl = wx.getStorageSync('url')
    // console.log(wx.getStorageSync('url'));
    getMusicPlayDetail(id).then(res=>{
      let playUrl = res.data.data[0].url;
      audio.title = "hello"
      audio.src = playUrl;
      let states = wx.getStorageSync('states')
      if(states){
        states[id] = true;
        for(let key in states){
          if(key !=id){
            states[key] = false
          }
        }
        wx.setStorageSync('states', states)
      }else{
        let states = {};
        states[id] = true;
        wx.setStorageSync('states', states)
      }
      this.setData({
        play:{
          id,imgUrl,playUrl
        }
      })
    })
  },
  handleMusic(){
    console.log(this.data.play.id);
    var id = this.data.play.id;
    var playState = this.data.playState;
    var states = wx.getStorageSync('states')
    if(playState){
      this.setData({
        playState:false
      })
      audio.pause();
      states[id] = false;
    }else{
      this.setData({
        playState:true
      })
      audio.play();
      states[id] = true;
    }
    wx.setStorageSync('states', states)
  }
})